package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.crm.entity.GwfaT;
import cn.e4j.modular.crm.entity.GwfalT;
import cn.e4j.modular.crm.service.GwfaTService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 费用编号维护作业
 * @author liuw
 * @date 2019/09/04
 */
@Controller
@RequestMapping("/crm/gwfi100")
public class Gwfi100Controller extends BaseController {
    @Autowired
    private GwfaTService gwfaTService;

    /**
     * 跳转至费用编号维护主界面
     *
     * @return
     */
    @RequiresPermissions("crm:gwfi100:init")
    @RequestMapping(value = {"", "/init"})
    public String index() {
        return "/pc/crm/gwfi100.html";
    }

    @RequiresPermissions("crm:gwfi100:init")
    @RequestMapping("/getList")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {

        Dto inDto = dtoWapper.getDto();
        Page<Object> page = dtoWapper.getPage();

        List<Map<String, Object>> list = gwfaTService.getGwfalist(page, inDto);

        return new PageReData(page, list);

    }

    @RequiresPermissions("crm:gwfi100:add")
    @RequestMapping("/add")
    @ResponseBody
    public ResponseData add(GwfaT gwfaT, GwfalT gwfalT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwfaT.setGWFAENT(shiroUser.getDBC());
        if (gwfaTService.existedTheParams(gwfaT)) {
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                    BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
        } else {
            gwfaTService.add(gwfaT, gwfalT);
            return SUCCESS_TIP;
        }
    }

    @RequiresPermissions("crm:gwfi100:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public ResponseData edit(GwfaT gwfaT, GwfalT gwfalT) {
        gwfaTService.edit(gwfaT, gwfalT);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:gwfi100:del")
    @RequestMapping("/del")
    @ResponseBody
    public ResponseData delRow(GwfaT gwfaT, GwfalT gwfalT) {
        gwfaTService.delete(gwfaT, gwfalT);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:gwfi100:del")
    @RequestMapping("/delBatch")
    @ResponseBody
    public ResponseData delRowsBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwfaT> gwfaTList = inDto.getReqList(GwfaT.class);
        gwfaTService.deleteBatch(gwfaTList);
        return SUCCESS_TIP;
    }
}
